package controller;

import com.alibaba.fastjson.JSON;
import entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by thinkjoy on 2017/7/13.
 */
@Controller
public class WsController {

    @RequestMapping(value = "/data",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String showdata(HttpServletRequest request, HttpServletResponse response){
        List<User> users=new ArrayList<>();
        Map<String,Object> map=new HashMap<>();
        User user=new User("许三胖","男","21");
        User user1=new User("许四胖","男","21");
        User user2=new User("许五胖","男","21");
        users.add(user);
        users.add(user1);
        users.add(user2);
        map.put("data",users);
        String json=JSON.toJSONString(map).toString();
        try {
            json=new String(json.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return json;
    }
}
